package com.dense.kuiniu.shelfcard.utils.timer;

import android.os.Handler;
import android.os.Message;

import java.util.Timer;
import java.util.TimerTask;

import androidx.annotation.NonNull;

/**
 * @author Administrator
 * @date 2020/8/5
 * 带重复次数的通用定时任务
 */
public class TimingTask {

    private static final int What_Finish = 100;
    private static final int What_Time = 101;
    //任务id
    private final int mTaskId;
    //单次执行持续时长
    private final double mLastTime;
    //单次已经消费的时间长度
    private int mHadExeTime = 0;
    //已经消费的的次数
    private int mHadExeCount = 0;
    //执行次数
    private final int mRepeatTime;
    private TimerObserver mTimerObserver;

    Timer timer = new Timer();
    private MyTask task;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            switch (msg.what) {
                case What_Finish:
                    finish();
                    break;
                case What_Time:
                    timer(msg.arg1);
                    break;
            }
        }
    };


    public class MyTask extends TimerTask {

        @Override
        public void run() {
            mHadExeTime++;
            Message message = Message.obtain();
            if (mHadExeTime == mLastTime) {
                message.what = What_Finish;
            } else {
                message.what = What_Time;
                message.arg1 = (int) (mLastTime - mHadExeTime);
            }
            mHandler.sendMessage(message);
        }
    }

    public TimingTask(int taskId, double intervalTime, int repeatTime) {
        mTaskId = taskId;
        mLastTime = intervalTime;
        mRepeatTime = repeatTime;
    }

    public int getHadExeCount() {
        return mHadExeCount;
    }

    public void setTimerObserver(TimerObserver timerObserver) {
        mTimerObserver = timerObserver;
    }

    public void removeObserver() {
        mTimerObserver = null;
    }

    public int getTaskId() {
        return mTaskId;
    }

    public double getLastTime() {
        return mLastTime;
    }

    public int getRepeatTime() {
        return mRepeatTime;
    }

    public void start() {
        mHadExeTime = 0;
        mHadExeCount = 0;
        startTask();
        if (mTimerObserver != null) {
            mTimerObserver.onStart();
        }
    }

    public void pause() {
        stopTask();
        if (mTimerObserver != null) {
            mTimerObserver.onPause();
        }
    }

    public void resume() {
        startTask();
        if (mTimerObserver != null) {
            mTimerObserver.onResume();
        }
    }

    public void stop() {
        stopTask();
        if (mTimerObserver != null) {
            mTimerObserver.onStop();
            mTimerObserver = null;
        }
    }

    private void timer(int count) {
        if (mTimerObserver != null) {
            mTimerObserver.onTime(count);
        }
    }

    private void finish() {
        mHadExeCount++;
        if (mHadExeCount >= mRepeatTime) {
            if (mTimerObserver != null) {
                mTimerObserver.onFinish(mHadExeCount);
                stopTask();
                mTimerObserver.onComplete();
            }
            mHadExeCount = 0;
        } else {
            mHadExeTime = 0;
            if (mTimerObserver != null) {
                mTimerObserver.onFinish(mHadExeCount);
            }
        }
    }

    private void startTask() {
        stopTask();
        timer.schedule(task = new MyTask(), 0, 1000);
    }

    private void stopTask() {
        if (task != null) {
            task.cancel();
            task = null;
        }
    }
}
